package com.yearner.reviewplan.user.model;

import apijson.orm.Visitor;
import com.yearner.reviewplan.common.util.MD5Util;
import com.yearner.reviewplan.user.commands.ChangePasswordCommand;
import com.yearner.reviewplan.user.exceptions.PasswordNotConfirmException;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * @author pengyg
 * @since 2019/10/12
 */
@Getter
@Setter
public class User implements Visitor<String> {
    private UserId userId;
    private String accountName;

    private String password;
//    private Account account;
    private Cycle cycle;

    private User(UserId id, String accountName,String password) {
        this.userId = id;
        this.accountName = accountName;
        this.password = password;
    }
    public void changeCycle(Double[] cycle) {
        this.cycle.changeCycles(cycle);

    }

    /**
     * 静态创建方法
     * 限定包内的 Factory 使用
     * @param id
     * @param accountName
     * @param password
     * @return
     */
    static User create(UserId id, String accountName,String password) {
        return new User(id,accountName,password);
    }

    public void changePassword(ChangePasswordCommand command) {
        String oldPwd = MD5Util.MD5EncodeUtf8Salt(command.getOldPassword());
        if (!this.password.equals(oldPwd)) {
            throw new PasswordNotConfirmException();
        }
        String newPwd = MD5Util.MD5EncodeUtf8Salt(command.getNewPassword());
        setPassword(newPwd);
    }

    @Override
    public String getId() {
        return this.userId.toString();
    }

    @Override
    public List<String> getContactIdList() {
        return null;
    }
}
